import java.io.*;
import java.util.*;
import org.jsoup.*;
import org.jsoup.nodes.*;
import org.jsoup.select.*;

public class Juzimi
{
	private String msg = null;
	private static int nums = 0;//统计得到的条数
	private int num=-1;
	private int maxnum=-1;//服务器总共条数
	private String foldpath = null;
	private String title = null;
	private String url = null;
	private Document doc;
	private static ArrayList<Juzi> al;
	public Juzimi(String url, String foldpath, int num)
	{
		this.url = url;
		this.foldpath = foldpath;
		this.num = num;
		start();
	}
	private void start()
	{
		nums = 0;
		//url https://www.mingyantong.com/article/44689 或m
		if (url.indexOf("https://m") != -1)
			url = url.replaceFirst("m", "www");
		if (!url.startsWith("https://www.mingyantong.com"))
		{
			System.out.println("非句子迷");
		}
		msg = Http.httpsRequest(url, "GET", null);
		Document doc = Jsoup.parse(msg);
		this.doc = doc;
		if (this.title == null)
			this.title = doc.getElementsByClass("taxtitlein").get(0).getElementsByTag("h1").get(0).text().trim();
		if (this.maxnum == -1)
		{ 
			String s = doc.getElementsByClass("xqnumfav").get(0).text().trim();
			if (s.indexOf("收录") == -1)
			{
				this.maxnum = Integer.parseInt(s.substring(s.indexOf("共") + 1, s.indexOf("个句子")));
			}
			else
			{
				this.maxnum = Integer.parseInt(s.substring(s.indexOf("共收录") + 3, s.indexOf("个")));
			}

		}
		al = new ArrayList<Juzi>();
		System.out.println("本次下载: " + title + "	共有" + maxnum + "条");
		get(url);
		if(num>maxnum)
			finish();
	}
	private void get(String url)
	{
		if (title != null)
		{
			msg = Http.httpsRequest(url, "GET", null);
			Document doc = Jsoup.parse(msg);
			this.doc = doc;
		}
		Elements e = doc.getElementsByClass("views-field-phpcode");
		//views-field-phpcode
		/*
		 <div class="views-field-phpcode"> 
		 <span class="field-content">
		 <div class="views-field-phpcode-1">
		 <a href="/ju/152475" title="查看本句" class="xlistju">惟沉默是最高的轻蔑。</a>
		 </div>
		 <div class="xqalllisbottom">
		 <div class="views-field-ops">
		 <span class="flag-action">喜欢(2.6万)</span>
		 </div>
		 <div class="views-field-comment-count">
		 <div class="node-nid">
		 152475
		 </div>
		 <div class="user-uid">
		 0
		 </div>
		 <a href="/comment/reply/152475#licommentbegin" class="comment-link" title="查看全部评论">评论(220)</a>
		 </div>
		 <div class="xqjmojushare">
		 <div class="xqjmojusharein">
		 <a class="alljmojusharelink" id="xjsha152475" href="#alljmojushare152475">分享</a>
		 <div class="alljmojusharecon" id="alljmojushare152475">
		 <div class="views-field-field-sns-value">
		 <div id="bdshare" class="bdshare_t bds_tools_32 get-codes-bdshare" data="{'pic':'https://file.mingyantong.com/weibopic/jrzomi.jpg','shp':'ceb1b810','ji':'152475'}">
		 <a class="bds_piclist" href="javascript:;">生成图片</a>
		 <a class="bds_tsina" href="javascript:;">新浪微博</a>
		 <a class="bds_mqq" href="javascript:;">QQ好友</a>
		 <a class="bds_qzone" href="javascript:;">QQ空间</a>
		 </div> 
		 <div id="xqjusosh152475">
		 <script type="text/javascript">  if(ifjzmjsh){	  xqjunshare("xqjusosh152475", 152475, "", "", "");  }</script>
		 </div>
		 </div>
		 </div>
		 </div>
		 </div>
		 <script type="text/javascript">if(isJZMApp){document.write("<div class='xqjmojushareapp'><a href='javascript:xqappsha(152475)' class='xqjuandroidsharemore'>分享</a></div>")}</script>
		 </div></span> 
		 </div>
		*/
		for (int i = 0;i < e.size();i++)
		{
			if (num > 0)
			{
				if (nums < num)
				{
					nums++;
				}
				else
				{
					finish();
					break;
				}
			}else{
				nums++;
			}
			Juzi j = new Juzi(e.get(i));
			System.out.println(j.getWords());
			this.al.add(j);
		}
		Element e2 = doc.getElementsByClass("pager").get(0);
		//第一种
		if (e2.toString().indexOf("首 页") != -1)
		{
			if (e2.getElementsByClass("pager-last").get(0).getElementsByTag("a").size() != 0)
			{
				get("https://www.mingyantong.com/" + e2.getElementsByClass("pager-last").get(0).getElementsByTag("a").get(0).attr("href").trim());
			}
		}
		else if (e2.getElementsByTag("li").get(2).getElementsByTag("a").size() != 0)
		{
			get("https://www.mingyantong.com/" + e2.getElementsByTag("li").get(2).getElementsByTag("a").get(0).attr("href").trim());
		}
	}
	/*
	 <ul class="pager">
	 <li class="pager-first first"><span>首 页</span></li>
	 <li class="pager-previous"><span>上一页</span></li>
	 <li class="pager-current"><span><input rel="nofollow" value="1/2" xqcurrendata="1/2" xqmaxdata="2"></span></li>
	 <li class="pager-next"><a href="/article/44689?page=1" title="去下一个页面" rel="next" class="active">下一页</a></li>
	 <li class="pager-last"><a href="/article/44689?page=1" class="active">末 页</a></li>
	 <li class="pager-confirm"><span>跳 转</span></li>
	 <li class="pager-cancel last"><span>取 消</span></li>
	 </ul>

	 <ul class="pager">
	 <li class="pager-current first">1</li>
	 <li class="pager-item"><a href="/article/44689?page=1" title="到  2 页" class="active">2</a></li>
	 <li class="pager-next last"><a href="/article/44689?page=1" title="去下一个页面" rel="next" class="active">下一页</a></li>
	 </ul>

	 <ul class="pager">
	 <li class="pager-previous first"><a href="/article/44689" title=" 返回上一个页面" rel="prev" class="active">前一页</a></li>
	 <li class="pager-item"><a href="/article/44689" title="到第 1 页" class="active">1</a></li>
	 <li class="pager-current last">2</li>
	 </ul>
	 */


	public void save()
	{
		File f = new File(foldpath);
		if (!f.canWrite())
		{
			System.out.println("文件夹不可写");
			return;
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0;i < al.size();i++)
		{
			sb.append(al.get(i).toString() + "\n");
		}
		String filepath = f.getAbsolutePath() + "/" + this.title.replaceAll("/", " ") + ".txt";
		write(filepath, sb.toString());
	}
	private void write(String filepath, String s)
	{
		try
		{
			BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filepath)));
			bw.write(s);
			bw.flush();
			bw.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	private void finish()
	{
		System.out.println("完成!");
		if (foldpath != null)
		{
			System.out.print("保存在: " + foldpath + "	");
		}
		System.out.println("本次下载了" + nums + "条");
	}
}
class Juzi
{
	private String words = null;
	private String author = null; //writer
	private String from = null; //active
	private String like = null;

	public Juzi(Element e)
	{
		if (e.getElementsByClass("xlistju").size() != 0)
			this.setWords(e.getElementsByClass("xlistju").get(0).text().trim());
		//views-field-field-oriarticle-value
		if (e.getElementsByClass("views-field-field-oriwriter-value").size() != 0)
			this.setAuthor(e.getElementsByClass("views-field-field-oriwriter-value").get(0).text().trim());
		if (e.getElementsByClass("active").size() != 0)
			this.setFrom(e.getElementsByClass("active").get(0).text().trim());
		else if(e.getElementsByClass("views-field-field-oriarticle-value").size()!=0){
			this.setFrom(e.getElementsByClass("views-field-field-oriarticle-value").get(0).text().trim());
		}
		if (e.getElementsByClass("flag-action").size() != 0)
		{
			String s = e.getElementsByClass("flag-action").get(0).text().trim();
			this.setLike(s.substring(3, s.lastIndexOf(")")));
		}

	}
	public void setWords(String words)
	{
		this.words = words;
	}

	public String getWords()
	{
		return words;
	}

	public void setAuthor(String author)
	{
		this.author = author;
	}

	public String getAuthor()
	{
		return author;
	}

	public void setFrom(String from)
	{
		this.from = from;
	}

	public String getFrom()
	{
		return from;
	}

	public void setLike(String like)
	{
		this.like = like;
	}

	public String getLike()
	{
		return like;
	}

	@Override
	public String toString()
	{
		// TODO: Implement this method
		String msg = new String();
		msg = this.getWords();
		if (this.getAuthor() != null)
			msg += ("	--" + this.getAuthor());
		if (this.getFrom() != null)
			msg += ("	--《" + this.getFrom() + "》");
		if (this.getLike() != null)
			msg += ("	" + this.getLike());
		return msg;
	}

}
